<template>
  <div>
    <el-card class="box-card">
      <el-form :inline="true" :model="formPoint" class="demo-form-inline">
        <el-form-item label="区域搜索：">
          <el-input v-model="formPoint.name" placeholder="请输入" icon="el-icon-circle-close" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" style="background-color:#5f84ff" @click="submit">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="box-card">
      <el-row>
        <el-button type="warning" icon="el-icon-circle-plus-outline" class="icon-add" @click="createPointFn">新建</el-button>
      </el-row>
      <el-table :data="pointList" style="width: 100%">
        <el-table-column type="index" label="序号"/>
        <el-table-column prop="name" label="区域名称" width="180" />
        <el-table-column prop="nodeCount" label="点位数" width="180" />
        <el-table-column prop="remark" label="备注说明" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="mini" @click="lookdialogFn(scope.row.id)">查看详情</el-button>
            <el-button size="mini" @click="handleEdit(scope.row.id)">修改</el-button>
            <el-button size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="1"
        :page-size="10"
        layout="prev, pager, next, jumper"
      />
    </el-card>
    <!-- 查看详情 弹窗-->
    <el-dialog
      title="区域详情"
      :visible.sync="lookdialog"
      close="lookdialogClose"
      width="40%"
    >
      <el-row>区域名称：{{ detailsList.name }}</el-row>
      <el-container>
        <el-aside width="100px">包含点位</el-aside>
        <el-main>
          <el-table :data="pointDetails">
            <el-table-column type="index" label="序号" width="50" />
            <el-table-column property="name" label="点位名称" width="120" />
            <el-table-column property="vmCount" label="设备数量" width="120" />
          </el-table>
        </el-main>
      </el-container>

    </el-dialog>
    <!-- 新建 弹窗 -->
    <createPoint :show="reateNewShow" @pointClose="closeNewPoint" @submit="submitCreate" />
    <!-- 修改弹窗 -->
    <RevisePoint :show="reviseShow" :fromdata="fromRevise" @close="reviseClose" @submit="confirmRevise" />
  </div>
</template>

<script>
import createPoint from './components/createPoint.vue'
import RevisePoint from './components/revisePoint.vue'
import { getPointApi, searchPointApi, getRegionIdApi, revisePointApi, createPointApi } from '@/api'
export default {
  components: {
    createPoint, RevisePoint
  },
  data() {
    return {
      formPoint: {
        name: ''
      },
      pointList: [], // 表单列表
      detailsList: [], // 详情列表
      total: '',
      pointDetails: [], // 点位详情
      lookdialog: false,
      reateNewShow: false,
      reviseShow: false, // 修改显示
      fromRevise: { // 修改表单
        name: '',
        remark: ''
      }
    }
  },
  created() {
    this.fatchGetPointList()
  },
  methods: {
    // 获取列表
    async fatchGetPointList(val) {
      const res = await getPointApi(val)
      this.pointList = res.currentPageRecords
    },
    async  submit() {
      this.fatchGetPointList({ name: this.formPoint.name })
    },
    // 点击新建
    createPointFn() {
      this.reateNewShow = true
    },
    // 关闭新建
    closeNewPoint() {
      this.reateNewShow = false
    },
    // 确认 新建
    async  submitCreate(val) {
      await createPointApi(val)
      this.closeNewPoint()
    },
    // 点击查看详情
    async lookdialogFn(val) {
      this.lookdialog = true
      const res = await getRegionIdApi(val)
      this.detailsList = res
      // 点位搜索
      const res2 = await searchPointApi(res.id)
      this.pointDetails = res2.currentPageRecords
    // console.log(this.pointDetails)
    },
    // 关闭查看详情
    lookdialogClose() {
      this.pointDetails = []
      this.lookdialog = false
    },
    // 点击修改
    async  handleEdit(val) {
      this.reviseShow = true
      const res = await getRegionIdApi(val)
      this.fromRevise = res
    },
    // 确认修改
    async confirmRevise(val) {
      await revisePointApi(val)
      this.reviseClose()
    },
    // 关闭修改
    reviseClose() {
      this.reviseShow = false
      this.fromdata = {}
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card{
  margin: 10px 0 10px 10px;
}
::v-deep .el-form-item__label{
  font-weight: 400;
  color: #606266;
}
.icon-add{
 background: linear-gradient(135deg,#ff9743,#ff5e20)!important;
}
</style>
